package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.java.*;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import static org.mybatis.generator.internal.util.JavaBeansUtil.getGetterMethodName;
import static org.mybatis.generator.internal.util.JavaBeansUtil.getSetterMethodName;

/**
 * @author wangfupeng
 */
public class MybatisPluginEqualsHashCode extends PluginAdapter {

    public MybatisPluginEqualsHashCode() {
    }

    /**
     * This plugin is always valid - no properties are required
     */
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> columns;
        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            columns = introspectedTable.getNonBLOBColumns();
        } else {
            columns = introspectedTable.getAllColumns();
        }
        generateEquals(topLevelClass, columns, introspectedTable);
        generateHashCode(topLevelClass, columns, introspectedTable);
        generateSetAllFieldsNotNull(topLevelClass, columns, introspectedTable);
        generateToString(topLevelClass, columns, introspectedTable);
        generateCopyAllFieldsNotNull(topLevelClass, columns, introspectedTable);
        generateCopyAllFields(topLevelClass, columns, introspectedTable);
        return true;
    }

    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        generateEquals(topLevelClass, introspectedTable.getPrimaryKeyColumns(),
                introspectedTable);
        generateHashCode(topLevelClass, introspectedTable
                .getPrimaryKeyColumns(), introspectedTable);

        return true;
    }

    @Override
    public boolean modelRecordWithBLOBsClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        generateEquals(topLevelClass, introspectedTable.getAllColumns(),
                introspectedTable);
        generateHashCode(topLevelClass, introspectedTable.getAllColumns(),
                introspectedTable);

        return true;
    }

    /**
     * Generates an <tt>equals</tt> method that does a comparison of all fields.
     * <p>
     * The generated <tt>equals</tt> method will be correct unless:
     * <ul>
     * <li>Other fields have been added to the generated classes</li>
     * <li>A <tt>rootClass</tt> is specified that holds state</li>
     * </ul>
     *
     * @param topLevelClass       the class to which the method will be added
     * @param introspectedColumns column definitions of this class and any superclass of this
     *                            class
     * @param introspectedTable   the table corresponding to this class
     */
    protected void generateEquals(TopLevelClass topLevelClass,
                                  List<IntrospectedColumn> introspectedColumns,
                                  IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setFinal(true);
        method.setReturnType(FullyQualifiedJavaType
                .getBooleanPrimitiveInstance());
        method.setName("equals"); //$NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType
                .getObjectInstance(), "that")); //$NON-NLS-1$
        if (introspectedTable.isJava5Targeted()) {
            method.addAnnotation("@Override"); //$NON-NLS-1$
        }

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        method.addBodyLine("if (this == that) {"); //$NON-NLS-1$
        method.addBodyLine("return true;"); //$NON-NLS-1$
        method.addBodyLine("}"); //$NON-NLS-1$

        method.addBodyLine("if (that == null) {"); //$NON-NLS-1$
        method.addBodyLine("return false;"); //$NON-NLS-1$
        method.addBodyLine("}"); //$NON-NLS-1$

        method.addBodyLine("if(!(that instanceof  " + topLevelClass.getType().getShortName() + ")){\n" +
                "            return false;\n" +
                "        }"); //$NON-NLS-1$

        StringBuilder sb = new StringBuilder();
        sb.append(topLevelClass.getType().getShortName());
        sb.append(" other = ("); //$NON-NLS-1$
        sb.append(topLevelClass.getType().getShortName());
        sb.append(") that;"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());

        boolean first = true;
        Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
        while (iter.hasNext()) {
            IntrospectedColumn introspectedColumn = iter.next();

            sb.setLength(0);

            if (first) {
                sb.append("return ("); //$NON-NLS-1$
                first = false;
            } else {
                OutputUtilities.javaIndent(sb, 1);
                sb.append("&& ("); //$NON-NLS-1$
            }

            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), introspectedColumn
                            .getFullyQualifiedJavaType());

            if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
                sb.append("this."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("() == "); //$NON-NLS-1$
                sb.append("other."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("())"); //$NON-NLS-1$
            } else {
                sb.append("this."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("() == null ? other."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("() == null : this."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("().equals(other."); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("()))"); //$NON-NLS-1$
            }

            if (!iter.hasNext()) {
                sb.append(';');
            }

            method.addBodyLine(sb.toString());
        }

        topLevelClass.addMethod(method);
    }

    /**
     * Generates a <tt>hashCode</tt> method that includes all fields.
     * <p>
     * Note that this implementation is based on the eclipse foundation hashCode
     * generator.
     *
     * @param topLevelClass       the class to which the method will be added
     * @param introspectedColumns column definitions of this class and any superclass of this
     *                            class
     * @param introspectedTable   the table corresponding to this class
     */
    protected void generateHashCode(TopLevelClass topLevelClass,
                                    List<IntrospectedColumn> introspectedColumns,
                                    IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setFinal(true);
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setName("hashCode"); //$NON-NLS-1$
        if (introspectedTable.isJava5Targeted()) {
            method.addAnnotation("@Override"); //$NON-NLS-1$
        }

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        method.addBodyLine("final int prime = 31;"); //$NON-NLS-1$
        method.addBodyLine("int result = 1;"); //$NON-NLS-1$

        StringBuilder sb = new StringBuilder();
        boolean hasTemp = false;
        Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
        while (iter.hasNext()) {
            IntrospectedColumn introspectedColumn = iter.next();

            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                    .getFullyQualifiedJavaType();

            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), fullyQualifiedJavaType);

            sb.setLength(0);
            if (fullyQualifiedJavaType.isPrimitive()) {
                if ("boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + ("); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("() ? 1231 : 1237);"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + "); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("();"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("char".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + "); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("();"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("double".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    if (!hasTemp) {
                        method.addBodyLine("long temp;"); //$NON-NLS-1$
                        hasTemp = true;
                    }
                    sb.append("temp = Double.doubleToLongBits("); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("());"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                    method
                            .addBodyLine("result = prime * result + (int) (temp ^ (temp >>> 32));"); //$NON-NLS-1$
                } else if ("float".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb
                            .append("result = prime * result + Float.floatToIntBits("); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("());"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("int".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + "); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("();"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("long".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + (int) ("); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("() ^ ("); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("() >>> 32));"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else if ("short".equals(fullyQualifiedJavaType.getFullyQualifiedName())) { //$NON-NLS-1$
                    sb.append("result = prime * result + "); //$NON-NLS-1$
                    sb.append(getterMethod);
                    sb.append("();"); //$NON-NLS-1$
                    method.addBodyLine(sb.toString());
                } else {
                    // should never happen
                    continue;
                }
            } else {
                sb.append("result = prime * result + (("); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("() == null) ? 0 : "); //$NON-NLS-1$
                sb.append(getterMethod);
                sb.append("().hashCode());"); //$NON-NLS-1$
                method.addBodyLine(sb.toString());
            }
        }

        method.addBodyLine("return result;"); //$NON-NLS-1$

        topLevelClass.addMethod(method);
    }


    protected void generateSetAllFieldsNotNull(TopLevelClass topLevelClass,
                                               List<IntrospectedColumn> introspectedColumns,
                                               IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setFinal(true);
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(topLevelClass.getType());
        method.setName("setAllFieldsNotNull");
        //method.addParameter(new Parameter(topLevelClass.getType(), "that"));

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        StringBuilder stringBuilder = new StringBuilder();
        Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
        while (iterator.hasNext()) {
            IntrospectedColumn introspectedColumn = iterator.next();

            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                    .getFullyQualifiedJavaType();

            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), fullyQualifiedJavaType);

            String setterMethod = getSetterMethodName(
                    introspectedColumn.getJavaProperty());

            stringBuilder.setLength(0);
            if (fullyQualifiedJavaType.isPrimitive()) {
                if ("boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(false);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(Byte.parseByte(\"0\"));}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("char".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(new Character('0'));}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("double".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Double.isNaN(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Double.isInfinite(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("float".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Float.isNaN(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Float.isInfinite(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("int".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("long".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0L);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("short".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(Short.parseShort(\"0\"));}");
                    method.addBodyLine(stringBuilder.toString());
                } else {
                    // should never happen
                    continue;
                }
            } else {
                if ("java.lang.Boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(false);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(Byte.parseByte(\"0\"));}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Character".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(new Character('0'));}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Double".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Double.isNaN(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Double.isInfinite(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Float".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Float.isNaN(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    stringBuilder.append("if(Float.isInfinite(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("())){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0.0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Integer".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Long".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(0L);}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.Short".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(Short.parseShort(\"0\"));}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.util.Date".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(java.util.Calendar.getInstance().getTime());}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("byte[]".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(new byte[]{0});}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.lang.String".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(\"\");}");
                    method.addBodyLine(stringBuilder.toString());
                } else if ("java.math.BigDecimal".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(BigDecimal.valueOf(0));}");
                    method.addBodyLine(stringBuilder.toString());
                } else {
                    stringBuilder.append("if(this.");
                    stringBuilder.append(getterMethod);
                    stringBuilder.append("()==null){this.");
                    stringBuilder.append(setterMethod);
                    stringBuilder.append("(\"\");}");
                    method.addBodyLine(stringBuilder.toString());
                }
            }
            if (introspectedColumn.getActualColumnName().equals("update_time")) {
                method.addBodyLine("this.setUpdateTime(java.util.Calendar.getInstance().getTime());");
            }
        }
        method.addBodyLine("return this;");
        topLevelClass.addMethod(method);
    }

    protected void generateToString(TopLevelClass topLevelClass,
                                    List<IntrospectedColumn> introspectedColumns,
                                    IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setFinal(true);
        method.addAnnotation("@Override");
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType
                .getStringInstance());
        method.setName("toString");

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.setLength(0);
        stringBuilder.append("String jsonString = \"{\";");
        method.addBodyLine(stringBuilder.toString());
        Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
        Boolean isFirstLine = true;
        while (iterator.hasNext()) {
            IntrospectedColumn introspectedColumn = iterator.next();

            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                    .getFullyQualifiedJavaType();

            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), fullyQualifiedJavaType);

            String setterMethod = getSetterMethodName(
                    introspectedColumn.getJavaProperty());
            if (isFirstLine == true) {
                isFirstLine = false;
            } else {
                stringBuilder.setLength(0);
                stringBuilder.append("jsonString = jsonString + \",\";");
                method.addBodyLine(stringBuilder.toString());
            }
            if (fullyQualifiedJavaType.isPrimitive()
                    || "java.lang.Boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Double".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Float".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Integer".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Long".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.Short".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    || "java.lang.BigDecimal".equals(fullyQualifiedJavaType.getFullyQualifiedName())
            ) {
                stringBuilder.setLength(0);
                stringBuilder.append("jsonString = jsonString + \"\\\"" + introspectedColumn.getJavaProperty() + "\\\":\"+this.");
                stringBuilder.append(getterMethod);
                stringBuilder.append("();");
                method.addBodyLine(stringBuilder.toString());
            } else if ("java.util.Date".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                stringBuilder.setLength(0);
                stringBuilder.append("jsonString = jsonString + \"\\\"" + introspectedColumn.getJavaProperty() + "\\\":\"+(this.");
                stringBuilder.append(getterMethod);
                stringBuilder.append("()==null?null:this.");
                stringBuilder.append(getterMethod);
                stringBuilder.append("().getTime())+\"\";");
                method.addBodyLine(stringBuilder.toString());
            } else {
                stringBuilder.setLength(0);
                stringBuilder.append("jsonString = jsonString + \"\\\"" + introspectedColumn.getJavaProperty() + "\\\":\\\"\"+this.");
                stringBuilder.append(getterMethod);
                stringBuilder.append("()+\"\\\"\";");
                method.addBodyLine(stringBuilder.toString());
            }
        }
        stringBuilder.setLength(0);
        stringBuilder.append("jsonString += \"}\";");
        method.addBodyLine(stringBuilder.toString());
        method.addBodyLine("return jsonString;");
        topLevelClass.addMethod(method);
    }


    protected void generateCopyAllFieldsNotNull(TopLevelClass topLevelClass,
                                                List<IntrospectedColumn> introspectedColumns,
                                                IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setFinal(true);
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setName("copyAllFieldsNotNull");
        method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
        method.addParameter(new Parameter(topLevelClass.getType(), "copyTo"));
        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);
        StringBuilder stringBuilder = new StringBuilder();
        Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
        stringBuilder.setLength(0);
        stringBuilder.append("if(this==null){return false;}");
        method.addBodyLine(stringBuilder.toString());
        stringBuilder.setLength(0);
        stringBuilder.append("if(copyTo==null){return false;}");
        method.addBodyLine(stringBuilder.toString());
        while (iterator.hasNext()) {
            IntrospectedColumn introspectedColumn = iterator.next();
            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                    .getFullyQualifiedJavaType();
            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), fullyQualifiedJavaType);
            String setterMethod = getSetterMethodName(
                    introspectedColumn.getJavaProperty());
            stringBuilder.setLength(0);
            stringBuilder.append("if(this.");
            stringBuilder.append(getterMethod);
            stringBuilder.append("()!=null){");
            stringBuilder.append("copyTo.");
            stringBuilder.append(setterMethod);
            stringBuilder.append("(");
            stringBuilder.append("this.");
            stringBuilder.append(getterMethod);
            stringBuilder.append("());}");
            method.addBodyLine(stringBuilder.toString());
        }
        method.addBodyLine("return true;");
        topLevelClass.addMethod(method);
    }

    protected void generateCopyAllFields(TopLevelClass topLevelClass,
                                         List<IntrospectedColumn> introspectedColumns,
                                         IntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setFinal(true);
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setName("copyAllFields");
        method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
        method.addParameter(new Parameter(topLevelClass.getType(), "copyTo"));
        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);
        StringBuilder stringBuilder = new StringBuilder();
        Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
        stringBuilder.setLength(0);
        stringBuilder.append("if(this==null){return false;}");
        method.addBodyLine(stringBuilder.toString());
        stringBuilder.setLength(0);
        stringBuilder.append("if(copyTo==null){return false;}");
        method.addBodyLine(stringBuilder.toString());
        while (iterator.hasNext()) {
            IntrospectedColumn introspectedColumn = iterator.next();
            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                    .getFullyQualifiedJavaType();
            String getterMethod = getGetterMethodName(
                    introspectedColumn.getJavaProperty(), fullyQualifiedJavaType);
            String setterMethod = getSetterMethodName(
                    introspectedColumn.getJavaProperty());
            stringBuilder.setLength(0);
            stringBuilder.append("copyTo.");
            stringBuilder.append(setterMethod);
            stringBuilder.append("(");
            stringBuilder.append("this.");
            stringBuilder.append(getterMethod);
            stringBuilder.append("());");
            method.addBodyLine(stringBuilder.toString());
        }
        method.addBodyLine("return true;");
        topLevelClass.addMethod(method);
    }


}
